//
//  LoginTextfieldView.m
//  GC_OC
//
//  Created by Tianbao Wang on 2022/5/27.
//

#import "LoginTextfieldView.h"

@implementation LoginTextfieldView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self showUIs];
    }
    return self;
}

- (void)showUIs {
    self.textfield = [[UITextField alloc] init];
    self.textfield.backgroundColor = [UIColor clearColor];
    self.textfield.layer.borderWidth = 1;
    self.textfield.layer.borderColor = XHUIColorFromRGB(0x979797).CGColor;
    self.textfield.font = [UIFont systemFontOfSize:16];
    self.textfield.textColor = [UIColor blackColor];
    [self.textfield setKeyboardType:UIKeyboardTypeASCIICapable];
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 12, 34)];
    self.textfield.leftView = leftView;
    self.textfield.leftViewMode = UITextFieldViewModeAlways;
    [self addSubview:self.textfield];
    [self.textfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(34);
        make.right.equalTo(self).offset(-54);
        make.left.equalTo(self).offset(107);
        make.centerY.equalTo(self);
    }];
    
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.textColor = [UIColor blackColor];
    self.nameLabel.textAlignment = NSTextAlignmentRight;
    self.nameLabel.font = [UIFont systemFontOfSize:16];
    [self addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(self.textfield.mas_left).offset(-7);
    }];
    
    self.resultImg = [[UIButton alloc] init];
    [self addSubview:self.resultImg];
    [self.resultImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.equalTo(self.textfield.mas_right).offset(15);
    }];
}

- (void)setIsPwd:(BOOL)isPwd
{
    _isPwd = isPwd;
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 34, 34)];
    UIButton *lookBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 34, 34)];
    [lookBtn setImage:[UIImage imageNamed:@"yanjing_bxianshi"] forState:UIControlStateNormal];
    [lookBtn setImage:[UIImage imageNamed:@"yanjing_xianshi"] forState:UIControlStateSelected];
    [lookBtn addTarget:self action:@selector(showpwd:) forControlEvents:UIControlEventTouchUpInside];
    [rightView addSubview:lookBtn];
    
    self.textfield.rightView = rightView;
    self.textfield.rightViewMode = UITextFieldViewModeAlways;
}

- (void)showpwd:(UIButton *)sender {
    sender.selected = !sender.selected;
    self.textfield.secureTextEntry = !sender.selected;
}
@end
